package top.hiai.service;

import java.util.List;

import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import top.hiai.model.Article;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top,开发者：黄康权
* @Introduction 类的介绍
*/

public class ArticleService {
	
	private Article dao = new Article().dao();
	
	/**
	 * 网站首页遍历文章列表,没有审核的文章不显示
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<Article> paginate(int pageNumber, int pageSize,String s_name) {
		if(s_name != null){
			return dao.paginate(pageNumber, pageSize, "select * ", "from  t_user ,t_article where  t_user.uid=t_article.user_id and t_article.state=? and t_article.name like '%"+s_name+"%' order by t_article.publish_date DESC",2);
		}else{
			return dao.paginate(pageNumber, pageSize, "select * ", "from  t_user ,t_article where  t_user.uid=t_article.user_id and t_article.state=? order by t_article.publish_date DESC",2);
		}
	}

	/**
	 * 网站后台遍历文章列表,显示所有的文章
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<Article> paginateAdmin(int pageNumber, int pageSize,String s_name,String s_state) {
		if(s_name != null){
			return dao.paginate(pageNumber, pageSize, "select * ", "from  t_user ,t_article where  t_user.uid=t_article.user_id and t_article.name like '%"+s_name+"%' order by t_article.publish_date DESC");
		}else if(s_state != null){
			return dao.paginate(pageNumber, pageSize, "select * ", "from  t_user ,t_article where  t_user.uid=t_article.user_id and t_article.state like '%"+s_state+"%' order by t_article.publish_date DESC");
		}else{
			return dao.paginate(pageNumber, pageSize, "select * ", "from  t_user ,t_article where  t_user.uid=t_article.user_id order by t_article.publish_date DESC");
		}
	}
	/**
	 * 网站前台根据文章的分类遍历文章列表
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<Article> paginateGetTypeId(int pageNumber, int pageSize,int typeId) {
		return dao.paginate(pageNumber, pageSize, "select t_article.aid,t_arctype.tid,t_article.name ,t_article.view,t_article.points,t_article.publish_date,t_user.username ,t_user.image_name ,t_arctype.name as typeName", "from t_article , t_user ,t_arctype  where  t_article.user_id=t_user.uid and t_article.type_id= t_arctype.tid and t_article.type_id= '"+typeId+"'  and t_article.state=? order by t_article.publish_date DESC",2);
	}
	/**
	 * 遍历文章列表，根据用户的ID，用于用户个人中心，文章的管理
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<Article> paginateGetUserId(int pageNumber, int pageSize,int uid,String s_name,String s_state) {
		
		if(s_name != null){
			return dao.paginate(pageNumber, pageSize, 
					"select t_article.aid , t_article.name ,t_article.points,t_article.publish_date,t_article.state,t_article.reason,t_arctype.name as typeName ", 
					"from t_article , t_user, t_arctype  "
					+ "where t_article.user_id= '"+uid+"' and t_article.type_id= t_arctype.tid and t_article.user_id=t_user.uid and t_article.name like '%"+s_name+"%' ");	
		}else if(s_state !=null){
			return dao.paginate(pageNumber, pageSize, 
					"select t_article.aid , t_article.name ,t_article.points,t_article.publish_date,t_article.state,t_article.reason,t_arctype.name as typeName ", 
					"from t_article , t_user, t_arctype  "
					+ "where t_article.user_id= '"+uid+"' and t_article.type_id= t_arctype.tid and t_article.user_id=t_user.uid and t_article.state like '%"+s_state+"%' ");	
		}else{
			return dao.paginate(pageNumber, pageSize,
					"select t_article.aid , t_article.name ,t_article.points,t_article.publish_date,t_article.state,t_article.reason,t_arctype.name as typeName ", 
					"from t_article , t_user, t_arctype  "
					+ "where t_article.user_id= '"+uid+"' and t_article.type_id= t_arctype.tid and t_article.user_id=t_user.uid order by t_article.publish_date DESC");	
		}
		
	}
	/**
	 * 根绝前台传过来的ID获取文章,用户文章的详情页面使用
	 * @param cid
	 * @return
	 */
	public Article getDetailArticle(int aid){
		return dao.findFirst(" select * from   t_user ,t_article where t_article.aid = '"+aid+"' and t_article.user_id = t_user.uid ");
	}
	 /**
	  * 获取热门文章，根据浏览量排序，默认获取15个
	  * @return
	  */
    public List<Article> getArticleHotList() {
        return dao.find(" select aid,name,view from t_article where state = 2 order by view desc,publish_date desc limit 15 ");
    }
	 /**
	  * 获取站长推荐文章，根据浏览量排序，默认获取10个
	  * @return
	  */
   public List<Article> getArticleTuijian() {
       return dao.find(" select aid,name,view from t_article where state = 2 and is_hot = true order by view desc,publish_date desc limit 10 ");
   }
    /**
     * 用户浏览文章，文章浏览量自动加1
     * @param cid
     */
    public void intArticleView(int aid) {
    	Db.update(" update t_article set view = view+1 where aid = ? ", aid);
    }
    /**
     * 删除文章
     * @param aid
     */
    public void deleteById(int aid) {
        dao.deleteById(aid);
    }
    /**
     * 根据文章的ID获取文章的信息，用户修改文章
     * @param uid
     * @return
     */
	public Article findById(int aid) {
		return dao.findById(aid);
	}
	/**
	 * 判断用户是否下载过文章资源了
	 * @param uid
	 * @param aid
	 * @return
	 */
	public Article getCountByUserIdAndArticleId(int uid,int aid){
		return dao.findFirst("select * from t_userxia where user_id=? and article_id=?",uid,aid);
	}
	
	
	public Ret getJiFenISNO(int uid,int aid){
		return null;
	}
	/**
	 * 所有的文章统计
	 */
	public Ret getArticleCounts() {
		Ret ret = Ret.by("title", "文章总数");
		Integer total = Db.queryInt("select count(*) from t_article");
		ret.set("total", total);
		return ret;
	}
	/**
	 * 待审核的文章统计
	 * @return
	 */
	public Ret getArticleStateCounts() {
		Ret ret = Ret.by("title", "待审核的文章总数");
		Integer total = Db.queryInt("select count(*) from t_article where state = '1'");
		ret.set("total", total);
		return ret;
	}
}
